import 'package:flutter/material.dart';
import '../widget/my_loading.dart';
import '../widget/error_page.dart';
typedef LoadDataFuture<T> = Future<T> Function(BuildContext context);
class MyFutureBuilder<T> extends StatefulWidget {
  MyFutureBuilder( {@required this.commonWidget,
    @required this.loadData,
    this.loadingWidget,
    this.errorWidget});
  final Widget loadingWidget;
  final Widget errorWidget;
  final NetNormalWidget<T> commonWidget;

  final LoadDataFuture<T> loadData;
  @override
  _MyFutureBuilderState createState() => _MyFutureBuilderState<T>();
}

class _MyFutureBuilderState<T> extends State<MyFutureBuilder<T>> with ErrorCallback{
  @override
  void initState() {
    super.initState();
    widget.loadData(context);
  }
  ///默认加载界面
  final defaultLoading =MyLoadding();

  @override
  void retryCall() {
    widget.loadData(context);
    setState(() {
      ///通知State重新构建界面需要
    });
  }



  @override
  Widget build(BuildContext context) {
    return FutureBuilder<T>(
      future: widget.loadData(context),
      builder: (BuildContext context, AsyncSnapshot<T> snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.none:
            break;
          case ConnectionState.waiting:
          case ConnectionState.active://
          return widget.loadingWidget ?? defaultLoading;
          case ConnectionState.done://-------- 完成态

            if (snapshot.hasError) {
//              if(snapshot.data.code==500&&snapshot.data.msg=='未能解析出token,请检查格式'){
//                Navigator.pushNamedAndRemoveUntil(context, '/login', (route) => false);
//              }
              ///网络出错
              if (widget.errorWidget != null) {

                ///自定义出错界面
                if (widget.errorWidget is NetErrorWidget) {
                  return widget.errorWidget;
                } else {
                  ///只自定义界面显示内容
                  return NetErrorWidget(
                    errorChild: widget.errorWidget,
                    callback: this,
                  );
                }

              } else {
                print('1234567');
                print(snapshot.data);
                print(snapshot.error.toString());

                ///选用默认出错界面
                return NetErrorWidget(
                  errorChild: ErrorPage(error:snapshot.error.toString()),
                  callback: this,
                );
              }
            }else{
//              return widget.commonWidget.buildWidget(snapshot.data);
              var res=snapshot.data;
              print(res);
              print('1111111');
//              if (res.code== 200) {
//                var result = res.result;
//                print(result);
//                return widget.commonWidget.buildWidget(snapshot.data);
//              }else{
//                return CustomLoaddingDialog(
//                    height: 150.0, width: 150, title: res.msg);
//              }
            }

            break;
          default:
            break;
        }
      },
    );
  }
}
///2.定义抽象类（数据正常展示Widget）
abstract class NetNormalWidget<T> extends StatelessWidget {
  final T bean;//通用数据类

  NetNormalWidget({this.bean,this.refresh});
  final Function refresh;
  @override
  Widget build(BuildContext context) {
    return buildWidget(bean);
  }
  ///给定义Widget赋值
  Widget buildWidget(T t);
}

//3.定义网络出错Widget以及对应点击回调
///net出错 回调
abstract class ErrorCallback {
  void retryCall();
}

///网络请求 失败 Widget
class NetErrorWidget extends StatelessWidget {
  final Widget errorChild;
  final ErrorCallback callback;

  NetErrorWidget({@required this.errorChild, this.callback});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: errorChild,
      onTap: () => callback?.retryCall(),
    );
  }
}